package cn.yqx.bankshop.oauth2.authentication.mobile;


import cn.yqx.bankshop.oauth2.store.VcodeManager;
import cn.yqx.bankshop.remote.SmsSendRemote;

import cn.yqx.bankshop.result.Result;
import cn.yqx.bankshop.result.ResultCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@RestController
@Slf4j
public class SmsController {

    @Value("${yqxSmsConfig.userid}")
    String userid ;
    @Value("${yqxSmsConfig.apikey}")
    String apikey ;
    @Value("${yqxSmsConfig.sign}")
    String sign ;
    @Value("${yqxSmsConfig.url}")
    String url ;

    private static final String SESSION_KEY = "IMAGE_CODE";
    private static final String SESSION_SMS_KEY = "SMS_CODE";
    private static final String SMS_CODE_MESSAGE = "您的验证码是：{code}。请在页面中提交验证码完成验证。此验证码10分钟内有效" ;
    private static final Integer EXPIRE_MINUTE_TIME =  5 ;
    private static final String MOBILE_KEY = "mobile" ;
    private static final String SMS_CODE_KEY = "{code}" ;
    private static final String SMS_SEND_CODE = "code" ;
    private static final String SMS_SEND_SMG = "msg" ;
    private static final String SMS_SEND_SUCCESS_CODE = "0" ;
    @Autowired
    private ValidateCodeGenerator smsCodeGenerator;

    @Autowired
    private VcodeManager vcodeManager ;

    @GetMapping("/code/sms")
    public Object createSmsCode(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletRequestBindingException {

        //生成随机验证码
        ValidateCode smsCode = smsCodeGenerator.createImageCode(request, response);
        //保存验证码，可以考虑用手机号保存
        String mobile = ServletRequestUtils.getRequiredStringParameter(request, MOBILE_KEY);
        // 保存验证码
        Result result  ;
        if(null == vcodeManager.getVcode(mobile)){
            vcodeManager.saveVcode(mobile, smsCode.getCode(), EXPIRE_MINUTE_TIME, TimeUnit.MINUTES);

            //模拟手机发送，此时调用短信服务商接口
            String message = SMS_CODE_MESSAGE.replace(SMS_CODE_KEY,smsCode.getCode()) ;
            SmsSendRemote smsSendRemote = new SmsSendRemote() ;
            JSONObject json =  smsSendRemote.yqxSmsSend(url,userid,sign,mobile,message) ;
            String smsSendCode = json.getString(SMS_SEND_CODE) ;

            if( !StringUtils.isEmpty(smsSendCode) && smsSendCode.equals(SMS_SEND_SUCCESS_CODE)){
                result = Result.success() ;
            }else{
                result = Result.failed(ResultCode.SMS_SEND_ERROR);
                result.setMsg(json.getString(SMS_SEND_SMG));
                result.setData(json);
            }
        }else{
            result = Result.failed(ResultCode.SMS_CODE_NO_EXPIRED) ;
        }
        return JSON.toJSON(result) ;
    }
}
